<?php

namespace App\Http\Controllers\Customer;

use App\Common\Traits\Model\ModelSearchTrait;
use App\Http\Controllers\Controller;
use App\Jobs\Finance\FinanceBillJob;
use App\Models\Finance\FinanceCustomerBill;
use Illuminate\Http\Request;

class CustomerBillController extends Controller
{
    use ModelSearchTrait;

    protected string $customerColumns='id,company_name,bill_settlement_group,advance_order';
    protected string $branchColumns='id,customer_bill_id,department_id,department_name,bill_imgs';

    //
    public function billList(Request $request, FinanceCustomerBill $customerBill){
        $customerBill=$this->singleModelSearch($request,$customerBill,$this->listCondition());
        $user=$request->user();
        $customerBill=$customerBill->where('bill_status',2)->where('customer_id',$user->id);
        $bills=$customerBill->with($this->listWith())->orderByDesc('id')->paginate($request->size??20);

        return $this->success($bills);
    }
    //
    public function listCondition(){
        return [
            ['bill_date','='],
        ];
    }
    //
    public function listWith(){
        return [
            'customer:'.$this->customerColumns,
        ];
    }

    //
    public function billDetail(Request $request){
        $user=$request->user();
        $bill=FinanceCustomerBill::with($this->detailWith())
            ->where('customer_id',$user->id)
            ->find($request->bill_id??0);
        if(empty($bill)){
            return $this->failed('id错误，无此账单',201);
        }
        return $this->success($bill);
    }
    //
    public function detailWith(){
        return [
            'accountBills',
            'customer:'.$this->customerColumns,
            'branchBills:'.$this->branchColumns,
        ];
    }


}
